package cn.gxa.edu_adm.service.impl;

import cn.gxa.edu_adm.bean.Student;
import cn.gxa.edu_adm.commn.model.ResultData;
import cn.gxa.edu_adm.dao.StudentDao;
import cn.gxa.edu_adm.dao.impl.StudentDaoImpl;
import cn.gxa.edu_adm.service.StudentService;

import java.time.LocalDateTime;
import java.util.*;

public class StudentServiceImpl  implements StudentService {
    StudentDao stduentDao=new StudentDaoImpl();
    @Override
    public ResultData findByManyWhere(Integer page, Integer limit, Integer id, String name, String phone, String idCard, Integer claId, Integer state) {

            //当前页的数据
        List<Student>   list=null;
        Long  count =0L  ;
        if(page==null && limit==null){//下载文件数据
              list=stduentDao.findByManyWhere(id, name, phone, idCard, claId, state);
              count=new Long(list.size());
        }else {
                list=stduentDao.findByManyWhere(page, limit, id, name, phone, idCard, claId, state);
            //总条数
                count=stduentDao.findByManyWhereCount(id, name, phone, idCard, claId, state);
        }
        if(count>0 && list.size()>0){
            return  ResultData.ok().data(list).count(count);
        }
        return ResultData.error().message("没有查询出数据");
    }

    @Override
    public ResultData deleteById(Integer id) {
        return    stduentDao.deleteById(id)>0?ResultData.ok().message("删除成功"):ResultData.error().message("数据被其他用户锁定,删除失败.");

    }

    @Override
    public ResultData deleteByIds(String arrId) {
        return    stduentDao.deleteByIds(arrId)>0?ResultData.ok().message("批量删除成功"):ResultData.error().message("数据被其他用户锁定,批量删除失败.");
    }

    @Override
    public ResultData update(Student student) {
        return    stduentDao.update(student)>0?ResultData.ok().message("修改成功"):ResultData.error().message("数据有误,修改失败");

    }

    @Override
    public ResultData insert(Student student) {
        return    stduentDao.insert(student)>0?ResultData.ok().message("添加成功"):ResultData.error().message("数据有误,添加失败");

    }

    @Override
    public ResultData batchAddStudent(List<Student> temp) {
        //temp  [][]
        Object  []  [] params =new Object[temp.size()][5];
        for (int  i=0;i<temp.size();i++) {
            params  [i][0]  =temp.get(i).getStuName();
            params  [i][1]  =temp.get(i).getStuSex();
            params  [i][2]  =temp.get(i).getStuBirthday();
            params  [i][3]  =temp.get(i).getStuPhone();
            params  [i][4]  =temp.get(i).getStuIdCard();
        }
        
        return stduentDao.batchAddStudent(params)>0?ResultData.ok().message("批量添加成功"):ResultData.error().message("数据有误,批量添加失败");
    }
/*--  20
-- 2029   2000  90  80  70  60以前 other
-- 2030   2010  00  90  80  70 以前  other
-- 2040    20 10  00  90  80以前 other
*/
    @Override
    public ResultData statisticsAge() {
        //获得年
        //int  year= Calendar.getInstance().get(Calendar.YEAR);
        int year= LocalDateTime.now().getYear();//2050
        //2050  2031 2021 2011 2001 1990 other
        String  []  title =new  String[6];
        title[5]="其他";
        //算出 要分析的年龄分层有哪些
        for(int  i=0;i<5;i++){
            int yearI=(i==0?year-20:Integer.parseInt(title[i-1])-10)/10*10;
            title[i]=yearI+"";
        }

        //得出年龄分布数据
        Object []  value=stduentDao.statisticsAge(title);
        for(int  i=0;i<5;i++){//2000
           title[i]=title[i].substring(title[i].length()-2);
        }
        System.out.println(Arrays.toString(title));
        System.out.println(Arrays.toString(value));

        Map<String,Object[]>  map=new HashMap<>();
        map.put("title",title);
        map.put("value",value);
        return  ResultData.ok().data(map);
    }
}
